//订单 projectwork
const router = require('koa-router')()

router.prefix('/order')

//backend
//获得全部数据
router.post('/allbackend', async (ctx, next) => {
    let resultuserid = await ctx.db.EXCUTE("SELECT (SELECT username FROM user ni WHERE n.userid=ni.userid) AS userinfo FROM orders n")
    let resultaddressid = await ctx.db.EXCUTE("SELECT (SELECT address FROM userreceive ni WHERE n.addressid=ni.addressid) AS addressinfo FROM orders n")
    let resultuser = await ctx.db.EXCUTE("SELECT (SELECT name FROM userreceive ni WHERE n.addressid=ni.addressid) AS userinfo1 FROM orders n")
    let result = await ctx.db.EXCUTE("SELECT n.*, (SELECT com_name FROM buy ni WHERE n.commodityid=ni.commodityid) AS commodityinfo FROM orders n")
    for (let i = 0; i < result.length; i++) {
        Object.assign(result[i], resultuserid[i]);
        Object.assign(result[i], resultaddressid[i]);
        Object.assign(result[i], resultuser[i]);
    }

    ctx.body = {
        code: 200,
        data: result
    }
})

//发货操作
router.post('/sendoutorderbackend', async (ctx, next) => {
    let orderid = ctx.request.body.orderid;
    let state = ctx.request.body.state;

    let sql0 = 'UPDATE orders SET state=? WHERE orderid=?'
    await ctx.db.EXCUTE(sql0, [state, orderid]);

    let result = await ctx.db.EXCUTE('SELECT * FROM orders ORDER BY orderid');

    ctx.body = {
        code: 200,
        data: result
    }
})

//order
//获取全部订单
router.get('/all', async (ctx, next) => {
    let userid = ctx.request.body.userid;
    let result = await ctx.db.EXCUTE('SELECT * FROM orders WHERE userid=' + userid);
    if (result[0]) {
        ctx.body = {
            code: 200,
            data: result,
            msg: 'order'
        }
    } else {
        ctx.body = {
            code: 200,
            data: result,
            msg: '暂时还没有订单喔'
        }
    }
})
// 获取全部订单
router.post('/alllist', async (ctx, next) => {
    console.log(ctx.request.body);
    let userid = ctx.request.body.userid;
    let sql = "select * from orders inner join buy on orders.commodityid = buy.commodityid where userid=?"
    let result = await ctx.db.EXCUTE(sql, [userid]);
    // for(let i=0; i<result.length; i++){
    //     let r = await ctx.db.EXCUTE('SELECT * FROM buydesc WHERE commodityid = ' + result[i].commodityid)
    //     result[i].img = r[0]
    // }
    ctx.body = {
        code: 200,
        data: result
    }
})
//获取订单
router.post('/single', async (ctx, next) => {
    console.log(ctx.request.body);
    let state = ctx.request.body.state;
    let userid = ctx.request.body.userid;
    console.log(state);
    let sql = "select * from orders inner join buy on orders.commodityid = buy.commodityid where state=? and userid=?"
    let result = await ctx.db.EXCUTE(sql, [state, userid]);
    ctx.body = {
        code: 200,
        data: result
    }
})
//根据orderid
router.post('/single/id', async (ctx, next) => {
    let orderid = ctx.request.body.orderid;
    let sql = "select * from orders inner join buy on orders.commodityid = buy.commodityid inner join userreceive on orders.addressid = userreceive.addressid where orderid=?"
    let result = await ctx.db.EXCUTE(sql, [orderid]);

    ctx.body = {
        code: 200,
        data: result
    }
})

router.post('/upload', async (ctx, next) => {
    console.log(ctx.request.body);
    let com_price = ctx.request.body.com_price
    let express_fee = ctx.request.body.express_fee
    let userid = ctx.request.body.userid
    let addressid = ctx.request.body.addressid
    let comnum = ctx.request.body.comnum
    let commodityid = ctx.request.body.commodityid
    let now = new Date().getTime()
    let ordernum = now + Math.round(Math.random() * (10000 - 1) + 1)
    let sql = "INSERT INTO orders (userid, addressid, comnum, comprice, commodityid, start_date, ordernum, sumprice, state) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
    let result = await ctx.db.EXCUTE(sql, [userid, addressid, comnum, com_price, commodityid, now, ordernum, comnum * com_price + express_fee, 0])

    ctx.body = {
        code: 200,
        data: result[0]
    }
})

// 确认收货, 确认评价
router.post('/confirm', async (ctx, next) => {
    console.log(ctx.request.body);
    let orderid = ctx.request.body.orderid
    let state = ctx.request.body.state
    let sql = "update orders set state=? where orderid=?"
    let result = await ctx.db.EXCUTE(sql, [state + 1, orderid])

    ctx.body = {
        code: 200,
        data: result
    }
})


module.exports = router